package cn.yto.print.config;

import cn.yto.print.config.i18n.I18nResourceManager;
import cn.yto.print.controller.IndexController;
import cn.yto.print.enums.MessageTypeEnum;
import cn.yto.print.system.SpringContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.websocket.CloseReason;
import javax.websocket.Session;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

public class PrintLinkedContext {

    private static final Logger logger = LoggerFactory.getLogger(PrintLinkedContext.class);

    private static final ConcurrentHashMap<String, Session> PRINT_SCOKET_MAP = new ConcurrentHashMap<>();

    public static Session getSession(String sessionId) {
        return PRINT_SCOKET_MAP.get(sessionId);
    }

    public static Map<String, Session> getAllSession() {
        return PRINT_SCOKET_MAP;
    }

    public static void addSession(String sessionId, Session session) {
        PRINT_SCOKET_MAP.put(sessionId, session);
        String message = String.format(I18nResourceManager.getInstance().get("index.console.session"), getSessionSize());
        SpringContextHolder.getBean(IndexController.class).addConsoleLog(message, MessageTypeEnum.SUCCESS);
        logger.info("前端打印通道连接建立，会话ID：{}，连接数 : {}", sessionId, getSessionSize());
    }

    public static void removeSession(String sessionId) {
        PRINT_SCOKET_MAP.remove(sessionId);
    }

    public static int getSessionSize() {
        return PRINT_SCOKET_MAP.size();
    }

}
